package www.wmx.com.xssc.domain.vo;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Data
public class CommonBeforeKJJL {
    @ApiModelProperty(value = "项目名称")
    String xmmc;
    @ApiModelProperty(value = "获奖时间")
    LocalDateTime hjsj;
    @ApiModelProperty(value = "奖项名称")
    String jxmc;
    @ApiModelProperty(value = "奖励登记")
    String jldj;
    @ApiModelProperty(value = "授奖单位")
    String sjdw;
    @ApiModelProperty(value = "相关获奖人")
    List<String> xghjr = new ArrayList<>();
    // 构造函数，用于简化初始化

    public CommonBeforeKJJL(String xmmc, LocalDateTime hjsj, String jxmc, String jldj, String sjdw, List<String> xghjr) {
        this.xmmc = xmmc;
        this.hjsj = hjsj;
        this.jxmc = jxmc;
        this.jldj = jldj;
        this.sjdw = sjdw;
        this.xghjr = xghjr;
    }

    // 解析器类
    public static List<CommonBeforeKJJL> parseAwards(String data) {
        List<CommonBeforeKJJL> awards = new ArrayList<>();
        String[] lines = data.split("\\r?\\n"); // 假设换行符是\n或\r\n
        lines = removeEmptyLines(lines,6);
        if(lines.length<6) return  null;
        for (int i = 0; i < lines.length; i += 6) {
            String xmmc = lines[i];
            LocalDateTime hjsj = LocalDate.parse(lines[i + 1], DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay();
            String jxmc = lines[i + 2];
            String jldj = lines[i + 3];
            String sjdw = lines[i + 4];
            List<String> xghjr = Arrays.asList(lines[i + 5].split("、")); // 假设只有一个获奖人
            awards.add(new CommonBeforeKJJL(xmmc, hjsj, jxmc, jldj, sjdw, xghjr));
        }
        return awards;
    }

    public static String[] removeEmptyLines(String[] lines,int removeLine) {
        // 使用ArrayList来动态地存储非空格行
        List<String> nonEmptyLines = new ArrayList<>();
        for (String line : lines) {
            // 检查行是否为null或者只包含空白字符
            if (line != null && !line.trim().isEmpty()) {
                nonEmptyLines.add(line.trim());
            }
        }
        // 将ArrayList转换回String数组
        return nonEmptyLines.subList(removeLine,nonEmptyLines.size()).toArray(new String[0]);
    }
}
